<template>
  <el-dialog
    title="添加商品"
    :visible="goodsVisible"
    @update:visible="v=>$emit('update:goodsVisible',false)"
  >
    <el-form>
      <el-form-item :label="'商品名称:'+$route.name">
        <el-input v-model="goodsForm.goodsName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="是否推荐:">
        <el-radio v-model="goodsForm.radio" label="1">推荐</el-radio>
        <el-radio v-model="goodsForm.radio" label="2">不推荐</el-radio>
      </el-form-item>
      <el-form-item label="店铺类型:">
        <el-select
          @change="changeShopType"
          v-model="goodsForm.shopTypeId"
          width="80px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in $store.state.shop.allshopTypeList"
            :key="item._id"
            :label="item.shopTypeName"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在店铺:">
        <el-select @change="changeShop" v-model="goodsForm.shopId" placeholder="请选择">
          <el-option
            v-for="item in $store.state.shop.shopListByTypeId"
            :key="item._id"
            :label="item.shopName"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品类别:">
        <el-select v-model="goodsForm.goodsTypeId" placeholder="请选择">
          <el-option
            v-for="item in $store.state.goods.shopByTypeId"
            :key="item._id"
            :label="item.goodsTypeName"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="店铺图片">
        <el-upload
          name="goodsPic"
          :auto-upload="false"
          :on-success="success"
          :data="goodsForm"
          ref="upload"
          :headers="{headerToken:$store.state.admin.token}"
          :limit="1"
          class="upload-demo"
          action="/elm/goodsList"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:goodsVisible',false)">取 消</el-button>
      <el-button type="primary" @click="$refs.upload.submit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "add-shop",
  props: {
    goodsVisible: {
      type: Boolean,
      default: false
    },
    shopTypeId: {
      type: String,
      default: ""
    },
    shopId: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      goodsForm: {
        goodsTypeName: "",
        goodsName: "",
        shopTypeId: "",
        shopId: "",
        goodsTypeId: "",
        radio: "1"
      }
    };
  },
  mounted() {
    console.log(this.$route.name);
    this.$store.dispatch("allgetShopTypeList");
    if (this.shopTypeId) {
      this.goodsForm.shopTypeId = this.shopTypeId;
      this.changeShopType(this.shopTypeId);
      if (this.shopId) {
        this.goodsForm.shopId = this.shopId;
        this.$store.dispatch("allgetgoodsTypeListByTypeId", this.shopId);
        // this.goodsForm.goodsTypeId = this.$store.state.goods.shopByTypeId[0].goodsTypeId;
      }
    }
  },
  methods: {
    //选择店铺名的时候触发事件,获取店铺类型相对应所有店铺
    changeShopType(shopTypeId) {
      this.$store.dispatch("allgetshopListByTypeId", shopTypeId);
      this.goodsForm.shopId = null;
    },
    //选择店铺类型
    changeShop(shopId) {
      //   console.log(shopId);
      this.$store.dispatch("allgetgoodsTypeListByTypeId", shopId);
      this.goodsForm.goodsTypeId = null;
    },
    success(res) {
      if (res.ok === 1) {
        // console.log(res);
        this.$emit("update:goodsVisible", false);
        // console.log(this.$route.name);
        if (this.$route.name !== "goodsList") {
          this.$router.history.push("/goodsList");
        }
        this.$store.dispatch("getGoodsList");
      }
    }
  }
};
</script>

